package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.GoodsModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索列表
 * Created by huyang on 2016/1/22 0022.
 */
public class GoodListAdapter extends BaseAdapter{

    private Context context;
    private List<GoodsModel> data;

    public GoodListAdapter(Context context) {
        this.context = context;
        data=new ArrayList<>();
    }

    public GoodListAdapter(Context context, List<GoodsModel> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(List<GoodsModel> data) {
        this.data.clear();
        this.data.addAll(data);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        GoodsModel model=data.get(position);
        final ViewHolder holder;
        if (converView==null){
            converView= LayoutInflater.from(context).inflate(R.layout.grid_item_good_list,null);
            holder=new ViewHolder();
            holder.name= (TextView) converView.findViewById(R.id.tv_goodsname);
            holder.price= (TextView) converView.findViewById(R.id.tv_goodsprice);
            holder.saleCount= (TextView) converView.findViewById(R.id.tv_salecount);
            holder.photo= (ImageView) converView.findViewById(R.id.iv_goods);
            converView.setTag(holder);
        }else{
            holder= (ViewHolder) converView.getTag();
        }
        holder.name.setText(model.getGoodsName()+"\n");
        holder.price.setText("¥"+model.getGoodsPrice());
        holder.saleCount.setText("月销量"+model.getSaleCount()+"笔");
        ImageLoader.getInstance().displayImage(model.getGoodsPhoto(),holder.photo);

        return converView;
    }
    class ViewHolder{
        TextView name;
        TextView price;
        TextView saleCount;
        ImageView photo;
    }
}
